package com.hxgz.steel.controller.backend;

import com.google.common.collect.Maps;
import com.hxgz.steel.common.Const;
import com.hxgz.steel.common.ResponseCode;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.domain.User;
import com.hxgz.steel.dto.GoodsTimeDto;
import com.hxgz.steel.domain.MerchantDto;
import com.hxgz.steel.domain.Page;
import com.hxgz.steel.dto.ProductTimeDto;
import com.hxgz.steel.service.BackendMerchantService;
import com.hxgz.steel.util.ExtractStringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(value = "店铺管理", description = "后台:店铺管理")
@RequestMapping("backendMerchant")
public class BackendMerchantController {

  @Autowired
  private BackendMerchantService backendMerchantService;

  @ApiOperation("店铺管理列表查询")
  @RequestMapping(value = "checkBackendMerchant.do", method = RequestMethod.POST)
  public ServerResponse<List<MerchantDto>> checkBackendMerchant(
      @ApiParam("商铺审核状态 0待审核 1审核未通过 2审核已通过 3已禁用") @RequestParam(required = false) Integer status,
      @ApiParam("提交开始时间") @RequestParam(required = false) String commitStartDate,
      @ApiParam("提交结束时间") @RequestParam(required = false) String commitEndDate,
      @ApiParam("认证开始时间") @RequestParam(required = false) String authenticationStartTime,
      @ApiParam("认证结束时间") @RequestParam(required = false) String authenticationEndTime,
      @ApiParam("账号") @RequestParam(required = false) String userName,
      @ApiParam("店铺名称") @RequestParam(required = false) String name,
      @ApiParam("现货商家") @RequestParam(required = false) Integer processStatus,
      @ApiParam("仓储加工") @RequestParam(required = false) Integer spotGoodsStatus,
      Page page, HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    HashMap<Object, Object> map = Maps.newHashMap();
    map.put("status", status);
    map.put("commitStartDate", commitStartDate);
    map.put("commitEndDate", commitEndDate);
    map.put("authenticationStartTime", authenticationStartTime);
    map.put("authenticationEndTime", authenticationEndTime);
    map.put("userName", userName);
    map.put("name", name);
    map.put("processStatus", processStatus);
    map.put("spotGoodsStatus", spotGoodsStatus);
    return backendMerchantService.checkBackendMerchant(map, page);
  }

  @ApiOperation("店铺id查询或审核状态修改")
  @RequestMapping(value = "updateStatus.do", method = RequestMethod.POST)
  public ServerResponse updateStatus(@RequestParam String id,
      @ApiParam("商铺审核状态 0审核通过 1审核未通过 3禁用 传status为修改审核状态 不传为查询") @RequestParam(required = false) Integer status,
      @ApiParam("年费") @RequestParam(required = false) String annualFee,
      @ApiParam("佣金") @RequestParam(required = false) String commission,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    if (StringUtils.isNotBlank(annualFee)&&StringUtils.isNotBlank(commission)){
      if (!ExtractStringUtils.isNumStr(annualFee) || !ExtractStringUtils.isNumStr(commission)) {
        return ServerResponse.createByErrorMessage("请输入纯数字");
      }
    }
    return backendMerchantService.updateStatus(id, status, annualFee, commission);
  }


  @ApiOperation("店铺管理-商品管理列表查询")
  @RequestMapping(value = "checkGoods.do", method = RequestMethod.POST)
  public ServerResponse<GoodsTimeDto> checkGoods(
      @ApiParam("商品状态") @RequestParam(required = false) Integer status,
      @ApiParam("创建开始时间") @RequestParam(required = false) String startDate,
      @ApiParam("创建结束时间") @RequestParam(required = false) String endDate,
      @ApiParam("商品编号") @RequestParam(required = false) String productCode,
      @ApiParam("店铺名称") @RequestParam(required = false) String merchantName,
      @ApiParam("品名") @RequestParam(required = false) String topName,
      @ApiParam("商品名称") @RequestParam(required = false) String goodName,
      @ApiParam("材质") @RequestParam(required = false) String materials, Page page,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    HashMap<Object, Object> map = Maps.newHashMap();
    map.put("status", status);
    map.put("startDate", startDate);
    map.put("endDate", endDate);
    map.put("productCode", productCode);
    map.put("merchantName", merchantName);
    map.put("topName", topName);
    map.put("goodName", goodName);
    map.put("materials", materials);
    return backendMerchantService.checkGoods(map, page);
  }

  @ApiOperation("店铺管理-商品管理详情查询")
  @RequestMapping(value = "checkGoodsById.do", method = RequestMethod.POST)
  public ServerResponse<ProductTimeDto> checkGoodsById(@RequestParam String id,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return backendMerchantService.checkGoodsById(id);
  }


}


